<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Notice;
use App\Models\NoticeReaded;
use Illuminate\Http\Request;
use App\Models\Authorization;
use App\Transformers\NoticeTransformer;
use Illuminate\Support\Facades\DB;
use Log;

class NoticeController extends BaseController
{
    /**
     *
     * 获取用户的通知消息列表
     * @param {Object} Request
     */
    public function index(Request $request)
    {
		$type = $request->get('type', -1);
        //当前页数 默认1
		$page = $request->get('page', 1);
		//每页的条数
		$perPage = 10;
		//计算每页分页的初始位置
		$offset = ($page * $perPage) - $perPage;
		if($type == -1){
			$notices = Notice::orderBy('id', 'desc')->paginate($perPage);
		}else{
			$notices = Notice::where('type', $type)->orderBy('id', 'desc')->paginate($perPage);
		}
		
		$user = $this->user();
		 //实例化LengthAwarePaginator类，并传入对应的参数
		$dataArry = $notices->toArray();
		$noticeIds = isset($dataArry['data']) ? field_vals($dataArry['data'], 'id') : [];
		if($user && $user->id){
			\Log::info('user id: '.\var_export($user->id, true).' noticeIds :'.\var_export($noticeIds, true));
			$readedList = NoticeReaded::where('user_id', $user->id)->whereIn('notice_id', $noticeIds)->get();
			$readedKv = [];
			foreach($readedList as $one){
				$readedKv[$one->notice_id] = $one;
			}
			\Log::info('readedKv: '.\var_export($readedKv, true));
			foreach($dataArry['data'] as $index => $one){
				if(isset($readedKv[$one['id']])){
					$dataArry['data'][$index]['readed'] = true;
					$dataArry['data'][$index]['deleted'] = $readedKv[$one['id']]->deleted;
				}
			}
		}
		
		return $this->returnSuccess($dataArry);
    }
    
    /**
     *
     * 获取通知消息id的详细内容
     * @param {Object} Request
     */
    public function detail(Request $request)
    {
        $id = $request->get('id');
		$detail = Notice::where('id', $id)->first();
    	
    	return $this->returnSuccess($detail);
    }
	
	/**
	 *
	 * 标志已读
	 * @param {Object} Request
	 */
	public function readed(Request $request)
	{
	    $id = $request->get('id');
		$detail = Notice::findOrFail($id);
		$user = $this->user();
		if($user->id){
			$exists = NoticeReaded::where('user_id', $user->id)->where('notice_id', $id)->first();
			if($exists){
				return $this->returnFail('已标志');
			}
			$data = [
				'user_id' => $user->id,
				'notice_id' => $id,
				'readed' => 1,
			];
			NoticeReaded::insert($data);
		}
		
		return $this->returnSuccess($detail);
	}
	
	/**
	 *
	 * 标志已删除
	 * @param {Object} Request
	 */
	public function deleted(Request $request)
	{
	    $id = $request->get('id');
		$detail = Notice::findOrFail($id);
		$user = $this->user();
		if($user->id){
			$exists = NoticeReaded::where('user_id', $user->id)->where('notice_id', $id)->first();
			if($exists && $exists['deleted']){
				return $this->returnFail('已删除');
			}
			$data = [
				'user_id' => $user->id,
				'notice_id' => $id,
				'readed' => 1,
				'deleted' => 1,
			];
			if($exists){
				NoticeReaded::where('id', $exists['id'])->update($data);
			}else{
				NoticeReaded::insert($data);
			}
			
		}
		
		return $this->returnSuccess($detail);
	}
}
